package com.atguigu.lease.web.api.service.impl;

import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.api.dto.RoomInfoSearchDto;
import com.atguigu.lease.web.api.interceptor.StgyApiAuthInpterceptor;
import com.atguigu.lease.web.api.mapper.*;
import com.atguigu.lease.web.api.service.BrowsingHistoryService;
import com.atguigu.lease.web.api.service.CityInfoService;
import com.atguigu.lease.web.api.service.RoomInfoService;
import com.atguigu.lease.web.api.vo.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import org.springframework.data.domain.Page;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Resource
    LeaseAgreementMapper leaseAgreementMapper;
    @Resource
    GraphInfoMapper graphInfoMapper;
    @Resource
    FacilityInfoMapper facilityInfoMapper;
    @Resource
    LabelInfoMapper labelInfoMapper;
    @Resource
    PaymentTypeMapper paymentTypeMapper;
    @Resource
    LeaseTermMapper leaseTermMapper;
    @Resource
    ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    AttrValueMapper attrValueMapper;

    @Resource
    FeeValueMapper feeValueMapper;
    @Resource
    BrowsingHistoryService browsingHistoryService;

    //首页查询房间数据 可以使用缓存
    @Override
    public Page<RoomItemVo> getPageItem(RoomInfoSearchDto searchDto) {
        Page<RoomItemVo> page = searchDto.getPage(RoomItemVo.class);
        List<RoomItemVo> roomItemVos =  baseMapper.selectRoomItemVoList(page , searchDto);
        page.setRecords(roomItemVos);
        return page;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //0、room_info
        RoomInfo roomInfo = this.getById(id);
        if(roomInfo==null){
            throw new StgyException(ResultCodeEnum.DATA_ERROR);
        }
        BeanUtils.copyProperties(roomInfo ,roomDetailVo );
        //1、是否入住：租约表:  如果查询到数据 表示该房间已被出租
        boolean existsFlag = leaseAgreementMapper.exists(Wrappers.lambdaQuery(LeaseAgreement.class)
                .eq(LeaseAgreement::getRoomId, id)
                .in(LeaseAgreement::getStatus, 1, 2, 5, 7)
                .last("limit 1"));
        roomDetailVo.setIsCheckIn(existsFlag);
        //5、房间图片列表
        List<GraphInfo> graphInfos = graphInfoMapper.selectByRoomId(id);
        roomDetailVo.setGraphVoList(graphInfos);
        //3、房间配套列表
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectByRoomId(id);

        roomDetailVo.setFacilityInfoList(facilityInfos);
        //4、房间标签列表
        List<LabelInfo> labelInfos = labelInfoMapper.selectByRoomId(id);

        roomDetailVo.setLabelInfoList(labelInfos);
        //6、房间租期列表
        List<LeaseTerm> leaseTerms = leaseTermMapper.selectByRoomId(id);

        roomDetailVo.setLeaseTermList(leaseTerms);
        //7、房间支付方式列表
        List<PaymentType> paymentTypes = paymentTypeMapper.selectByRoomId(id);

        roomDetailVo.setPaymentTypeList(paymentTypes);
        //2、公寓表
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        // 2.1 查询公寓数据
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        BeanUtils.copyProperties(apartmentInfo , apartmentItemVo);
        // 2.2 查询公寓的图片数据
        List<GraphInfo> apartmentGraphInfos = graphInfoMapper.selectList(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId, roomInfo.getApartmentId())
                .eq(GraphInfo::getItemType, 1));

        apartmentItemVo.setGraphVoList(apartmentGraphInfos);
        // 2.3 查询公寓的标签列表
        List<LabelInfo> apartmentLabelInfos = labelInfoMapper.selectByApartmentId(roomInfo.getApartmentId());

        apartmentItemVo.setLabelInfoList(apartmentLabelInfos);
        // 2.4 查询公寓中所有房间的最低租金
        BigDecimal minRent = baseMapper.selectMinRentByAId(roomInfo.getApartmentId());

        apartmentItemVo.setMinRent(minRent);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
//        //8、房间的属性值
        List<AttrValueVo> attrValueVos   = attrValueMapper.selectAttrValueVosByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVos);
//        //9、房间所属公寓的杂费列表
        List<FeeValueVo> feeValueVos   = feeValueMapper.selectFeeValueVosByApartmentId(roomInfo.getApartmentId());
        roomDetailVo.setFeeValueVoList(feeValueVos);

        //异步保存浏览记录
        browsingHistoryService.saveOrUpdateAsync(StgyApiAuthInpterceptor.THREAD_LOCAL.get().getId()
            ,id);
        return roomDetailVo;
    }
}
